/*
 * Copyright 2021 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.genersoft.iot.vmp.vmanager.record;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author rox_p
 * @date 2021/9/26 15:25
 */
public class TransPortData extends Thread {

    Socket getDataSocket;
    Socket putDataSocket;

    public TransPortData(Socket getDataSocket, Socket putDataSocket) {
        this.getDataSocket = getDataSocket;
        this.putDataSocket = putDataSocket;
    }

    public void run() {
        boolean isRunning = true;
        InputStream in = null;
        OutputStream out = null;
        try {
            in = getDataSocket.getInputStream();
            out = putDataSocket.getOutputStream();

        } catch (Exception e) {
            e.printStackTrace();
        }
        while (isRunning) {

            try {
                // 读入数据
                byte[] data = new byte[400];

                int readlen = in.read(data);
                System.out.println("发送消息长度" + readlen);

                // 如果没有数据，则暂停
                if (readlen <= 0) {
                    Thread.sleep(300);
                    return;
                }
                RtspPacketEncode rtspPacketEncode = new RtspPacketEncode();
                rtspPacketEncode.packet_g711a(data, data.length, out);
//                out.flush();
            } catch (Exception e) {
                isRunning = false;
                e.printStackTrace();
            }
        }
        try {
            System.out.println("關閉socket");
            if (out != null) {
                out.close();
            }
            if (in != null) {
                in.close();
            }
            if (putDataSocket != null) {
                putDataSocket.close();
            }
            if (getDataSocket != null) {
                getDataSocket.close();
            }
            //清除緩存
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}